package learnJava.puzzlers.exceptinPuzzler;

public class ExecptionsPuzzlers {
//    public static class UnwelcomGuest {
//        public static final long GUEST_USER_ID = -1;
//        private static final long USER_ID;
//
//        static {
//            try {
//                USER_ID = getUserIDFromEnvironment();
//            } catch (IdUnavailabeException e) {
//                USER_ID = GUEST_USER_ID; // 编译器无法判断
//                System.out.println("logging in as guest");
//            }
//        }
//
//        private static long getUserIDFromEnvironment() throws IdUnavailabeException {
//            throw new IdUnavailabeException();
//        }
//
//
//    }

    public static class UnwelcomGuestChange {
        public static final long GUEST_USER_ID = -1;
        private static final long USER_ID = getUserIdOrGuest();

        private static long getUserIdOrGuest() {
            try {
                return getUserIDFromEnvironment();
            } catch (IdUnavailabeException e) {
                System.out.println("logging in as guest");
                return GUEST_USER_ID;
            }

        }

        private static long getUserIDFromEnvironment() throws IdUnavailabeException {
            throw new IdUnavailabeException();
        }

    }

    public static class HelloGoodbye{
        public static void main(String[] args) {
            System.out.println("Hello World");
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Goodbye world");
                }
            }));
            System.exit(0);
        }
    }
}

class IdUnavailabeException extends Exception {
}